import ajax from "./ajax";
import record from "./record";
import utils from "./utils";
import godebug from './config' 

window.addEventListener('error', function(a) {

    notice({
        message: a.message,
        filename: a.filename,
        stack: a.error && a.error.stack,
        name: '脚本发生错误',
        colno: a.colno,
        lineno: a.lineno,
        type: a.type,
    })
})

function error(data) {
    data = utils.extend(data, {
        breadcrumbs: record.breadcrumbs.get()
    })
    ajax({
        method: 'post',
        url: godebug.url+'/error/report',
        data: data,
        callback: function() {
            console.log('错误上报完毕')
        }
    })
}

function getData() {
    return {
        title: document.title,
        name: '',
        appKey: godebug.apikey,
        notifierVersion: godebug.notifierVersion,
        url: location.href,
        time: utils.getTime(),
        platform: navigator.platform,
        lang: navigator.language,
        message: '',
        userAgent: navigator.userAgent
    }
}

function notice(msg) {

    msg = utils.extend({}, getData(), msg)
    error(msg)
    console.log('请查看邮件')
    record.breadcrumbs.clear();
    // console.log(notice)
}
godebug.url = godebug.url||"http://rmos.lsdfly.com";
function performance(msg) {
    console.log('准备上报性能')
    let data = utils.extend(getData(), {
        performance: msg
    })
    ajax({
        method: 'post',
        url: godebug.url+'/performance/report',
        data: data,
        callback: function() {
            console.log('上报性能完毕')
        }
    })
}
export default {
    error,
    notice: [],
    performance
}